import { _decorator, Component, Node, Sprite, Tween, tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('BCDynamicProgressImage')
export class BCDynamicProgressImage extends Sprite {
	private m_basePercent: number = 0;
	private m_action: Tween<{ num: number }> = null;

	_updatePercent(finalPercent: number, time: number = 0.4) {
		this.m_action && this.m_action.stop();
		this._setPercent(this.m_basePercent);

		const obj = {
			num: this.m_basePercent
		};
		this.m_basePercent = finalPercent;
		this.m_action = tween(obj).to(time, { num: finalPercent }, {
			progress: (start: number, end: number, current: number, ratio: number) => {
				const offset = start + (end - start) * ratio;
				this._setPercent(offset);
				return offset;
			},
			onComplete: () => {
				this._setPercent(finalPercent);
			}
		});
		this.m_action.start();
	}

	private _setPercent(percent: number){
		this.fillRange = percent;
	}
}

